<template>
  <div>
    <el-card class="box-card" shadow="never" border="false">
      <el-form :model="queryObj" ref="queryObj" label-width="100px">
        <div class="filter-content">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="标签名称：" prop="tags">
                <el-input
                  style="width: 220px"
                  placeholder="请输入"
                  class="filter-item"
                  v-model="queryObj.tags"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="城市：" prop="city">
                <el-select
                  class="filter-item"
                  style="width: 220px"
                  v-model="queryObj.city"
                  @change="chooseCity"
                >
                  <el-option
                    v-for="item in citySelect.cityList"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item label="地区：" prop="area">
                <el-select
                  class="filter-item"
                  style="width: 220px"
                  v-model="queryObj.area"
                >
                  <el-option
                    v-for="item in citySelect.cityDate"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item label="企业简称：" prop="shortName">
                <el-input
                  v-model="queryObj.shortName"
                  style="width: 220px"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="状态：" prop="state">
                <el-select
                  class="filter-item"
                  style="width: 220px"
                  v-model="queryObj.state"
                >
                  <el-option
                    v-for="item in status"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item>
                <el-button @click="clear">清除</el-button>
                <el-button type="primary" @click="searchName">搜索</el-button>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-button
                style="float: right"
                type="success"
                @click="open('add')"
              >
                <svg-icon icon-class="edit"></svg-icon> 新增用户</el-button
              >
            </el-col>
          </el-row>
        </div>
      </el-form>
      <!-- 搜索部分 end -->
      <!-- 消息记录 -->
      <el-alert
        :title="`共 ${total} 条记录`"
        effect="dark"
        :closable="false"
        show-icon
        type="info"
      >
      </el-alert>
      <!-- 表格部分 -->
      <el-table
        :data="tableData"
        v-loading="loading"
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column align="center" label="序号" prop="id">
        </el-table-column>
        <el-table-column align="center" label="企业编号" prop="number">
        </el-table-column>
        <el-table-column align="center" label="企业简称" prop="company">
        </el-table-column>
        <el-table-column label="标签" prop="tags"> </el-table-column>
        <el-table-column align="center" label="创建者" prop="creatorID">
        </el-table-column>
        <el-table-column align="center" label="创建日期" prop="addDate">
        </el-table-column>
        <el-table-column align="center" label="备注" prop="remarks">
        </el-table-column>
        <el-table-column align="center" label="状态" prop="state">
          <template slot-scope="scope">
            <span v-if="scope.row.state">启用</span>
            <span v-else>禁用</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="操作"
          width="280"
          class-name="small-padding fixed-width"
        >
          <template #default="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              plain
              @click="open('edit', row.id)"
            ></el-button>
              <el-tooltip  class="item" effect="dark" :content="row.state?'禁用':'启用'" placement="top">
              <el-button
              @click="changeState(row.id,row.state)"
                type="success"
                icon="el-icon-check"
                circle
                v-if="!row.state"
              ></el-button>
              <el-button
              @click="changeState(row.id,row.state)"
                type="warning"
                icon="el-icon-close"
                circle
                v-else
              ></el-button>
    </el-tooltip>

            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="del(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <PageTool
          :total="total"
          :paginationPage="queryObj.page"
          :paginationPagesize="queryObj.pagesize"
          @pageChange="onPageChange"
          @pageSizeChange="onPageSizeChange"
        ></PageTool>
      </div>
    </el-card>
        <CompanysAdd
         ref="editUser"
         :titleInfo="titleInfo"
         :formBase="formBase"
         @newDataes="newDataes"
        ></CompanysAdd>
  </div>
</template>

<script>
import CompanysAdd from '@/module-hmmm/components/companys-add'
import PageTool from '@/module-manage/components/page-tool'
import { list, remove, disabled, detail } from '@/api/hmmm/companys.js'
import { provinces, citys } from '@/api/hmmm/citys.js'
export default {
  components: {
    PageTool,
    CompanysAdd
  },
  data () {
    return {
      type: '',
      loading: true,
      tableData: [],
      total: 0,
      queryObj: {
        page: 1,
        pagesize: 10
      },
      titleInfo: {
        text: '',
        pageTitle: '用户'
      },
      citySelect: {
        cityList: [],
        cityDate: []
      },
      status: [
        { value: 1, label: '启用' },
        { value: 0, label: '禁用' }
      ],
      formBase: {
        shortName: '',
        isFamous: false,
        company: '',
        province: '',
        city: '',
        tags: '',
        remarks: '',
        id: 0,
        number: ''
      }
    }
  },

  async created () {
    this.citySelect.cityList = provinces()
    this.initData()
  },

  methods: {
    async initData () {
      const { data } = await list(this.queryObj)
      this.total = data.counts
      this.tableData = data.items
      this.loading = false
    },
    onPageSizeChange (val) {
      this.queryObj.pagesize = val
      this.queryObj.page = 1
      this.initData()
    },
    onPageChange (val) {
      this.queryObj.page = val
      this.initData()
    },
    async changeState (id, state) {
      const obj = { id, state }
      if (state === 0) {
        obj.state = 1
      } else if (state === 1) {
        obj.state = 0
      }
      await disabled(obj)
      this.initData()
    },
    // 搜索
    searchName () {
      this.initData()
    },
    // 清除
    clear () {
      this.$refs.queryObj.resetFields()
      this.initData()
    },
    // 打开弹窗
    async open (type, id, number) {
      this.type = type
      this.titleInfo.text = this.text
      this.$refs.editUser.dialogFormV()
      for (const key in this.formBase) {
        if (key !== 'isFamous') {
          this.formBase[key] = ''
        }
      }
      if (this.type === 'edit') {
        const obj = { id }
        const { data } = await detail(obj)
        for (const key in this.formBase) {
          this.formBase[key] = data[key]
        }
      }
    },
    // 选择城市
    chooseCity (e) {
      this.citySelect.cityDate = citys(e)
      // this.queryObj.area = this.citySelect.cityDate[0]
    },
    // 添加成功后
    newDataes () {
      this.initData()
    },
    async del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const obj = { id }
        await remove(obj)
        this.initData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
  computed: {
    text () {
      if (this.type === 'add') return '创建'
      if (this.type === 'edit') return '编辑'
      else return 'add'
    }
  }
}
</script>

<style scoped lang="less">
/deep/ .el-button--medium.is-circle {
  margin-right: 5px;
}
/deep/ .el-alert--info.is-dark {
  background-color: #f4f4f5;
  color: #909399;
}
/deep/ .el-table th {
  background-color: #fafafa;
}
.box-card {
  margin: 10px auto;
  width: 95%;
  padding: 20px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  -webkit-transition: 0.3s;
  transition: 0.3s;
  .pagination {
    margin-top: 10px;
    float: right;
  }
}
.header {
  margin-bottom: 10px;
  .search {
    width: 200px;
    margin-right: 10px;
  }
}
</style>
